import Mock from 'mockjs'

var { newsList } = Mock.mock({
    newsList:[
        {
            "id":"@id",
            "xuehao":18101870,
            "name":"谭同学",
            "zhuanye":"软件工程",
            "ipone":1881067135,
            "biye":2022,
            "gangwei":"前端开发",
            "money":8000,
            "jiuye":"是",
            "manyi":"满意",
            "xiangxi":"",
            "pipei":"是"
        },
        {
            "id":"@id",
            "xuehao":18151890,
            "name":"李同学",
            "zhuanye":"计算机与科学",
            "ipone":1881078135,
            "biye":2020,
            "gangwei":"销售",
            "money":5003,
            "jiuye":"是",
            "manyi":"不满意",
            "xiangxi":"学校开展的课程太多后，与市场上使用到的最新技术有严重的版本问题，教学不够深入",
            "pipei":"不是"
        },
        {
            "id":"@id",
            "xuehao":18346813,
            "name":"王同学",
            "zhuanye":"大数据",
            "ipone":1881067135,
            "biye":2018,
            "gangwei":"网络直播",
            "money":6000,
            "jiuye":"是",
            "manyi":"不满意",
            "xiangxi":"出到社会后，发现自己所学的专业知识太过单薄，无法从事专业相关工作",
            "pipei":"不是"
        },
        {
            "id":"@id",
            "xuehao":18651576,
            "name":"陈同学",
            "zhuanye":"人工智能",
            "ipone":1881067135,
            "biye":2019,
            "gangwei":"java开发",
            "money":7500,
            "jiuye":"是",
            "manyi":"不满意",
            "pipei":"是"
        }
    ]
})

// 获取用户信息列表
function getList() {
    // 若 localStorage 没有数据，则将 Mock 的数据存入
    if (!localStorage.getItem('userlist')) {
      localStorage.setItem('userlist',JSON.stringify(newsList))
    }
    // 每次获取数据时，再从 localStorage 中拉取数据
    var userlist = JSON.parse(localStorage.getItem('userlist'))
    return userlist
  }
   
  // 获取单个用户信息
  function getUser(options) {
    // 先从 localStorage 中拉取数据
    var userlist = JSON.parse(localStorage.getItem('userlist'))
    // 遍历数组，返回id 与传来 id 相当的一个对象
    for( let index in userlist) {
      if (userlist[index].id == options.body) {
        var user = userlist[index]
        return user
        
      }
    }
  }

  function getUserr(options) {
    // 先从 localStorage 中拉取数据
    var userlist = JSON.parse(localStorage.getItem('userlist'))
    // 遍历数组，返回id 与传来 id 相当的一个对象
    for( let index in userlist) {
      if (userlist[index].xuehao == options.body) {
        var user = userlist[index]
        return user
        
      }
    }
  }
   
  // 删除用户信息
  function deleteUser(options) {
    // 先从 localStorage 中拉取数据
    var userlist = JSON.parse(localStorage.getItem('userlist'))
    // 根据传递的 id 删除 用户
    for( let index in userlist ) {
      if (userlist[index].id === options.body) {
        userlist.splice(index,1)
        localStorage.setItem('userlist', JSON.stringify(userlist))
      }
    }
    return {
      data: '用户删除成功'
    }
  }
   
  // 添加用户信息
  function addUser(options) {
    // 先从 localStorage 中拉取数据
    var userlist = JSON.parse(localStorage.getItem('userlist'))
    // 获取传入用户信息对象，是一个字符串，需要转化为对象
    var user = JSON.parse(options.body)
    // 使用 mock 随机生成一个 id
    user.id = Math.random()*1+3
    // 将 user 插入到 userlist 中
    userlist.push(user)
    // 重新将 userlist 存入 localStorage 中
    localStorage.setItem('userlist', JSON.stringify(userlist))
    return {
      data: '用户添加成功'
    }
  }
   
  // 更新用户信息
  function updateUser(options) {
    // 先从 localStorage 中拉取数据
    var userlist = JSON.parse(localStorage.getItem('userlist'))
    var user = JSON.parse(options.body)
    // 遍历 userlist 根据传入对象的 id 更新用户信息
    for ( let index in userlist ) {
   
      if ( userlist[index].id === user.id ) {
        userlist[index] = user
      }
    }
    localStorage.setItem('userlist', JSON.stringify(userlist))
    return {
      data: '用户更新成功'
    }
  }
   
  // 制作各个接口
  Mock.mock('/getlist', 'get', getList)
  Mock.mock('/getuser', 'post', getUser)
  Mock.mock('/getuserr', 'post', getUserr)
  Mock.mock('/deleteuser', 'post', deleteUser)
  Mock.mock('/adduser', 'post', addUser)
  Mock.mock('/updateuser', 'post', updateUser)

/*const express = require('express')
const app = express();
const studentLogin = require("./student_login.json")
const adminLogin = require("./admin_login.json")

app.get("/login",(req,res)=>{
  const user = url.parse(req.url,true).query.user;
  if(user == 'admin'){
    res.send(adminLogin)
  }else{
    res.send(studentLogin)
  }
})*/

export default Mock
   
  
